<template>
  <div class="app-container">
    <div class="title">社团管理</div>
    <el-card shadow="never" class="table-container">
      <template #header>
        <el-form
          ref="queryFormRef"
          :model="queryParams"
          :inline="true"
          style="margin-top: 10px"
        >
          <el-form-item prop="keywords" label="名称">
            <el-input
              v-model="queryParams.keywords"
              placeholder="输入名称"
              clearable
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="handleQuery"
              ><i-ep-search />搜索</el-button
            >

            <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
          </el-form-item>

          <el-form-item>
            <el-button type="success" @click="openDialog()"
              ><i-ep-plus />新增</el-button
            >
          </el-form-item>
        </el-form>
      </template>
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :stripe="true"
        size="large"
      >
        <el-table-column align="center" label="序号" type="index" width="100" />
        <el-table-column align="center" label="社团名称" prop="communityName" />
        <el-table-column
          align="center"
          label="社团社长"
          prop="communityPresident"
        />
        <el-table-column
          align="center"
          label="社团简介"
          prop="communityDetail"
        />
        <el-table-column align="center" label="社团成员数" prop="memberNumber">
          <template #default="scope">
            <el-tooltip
              class="box-item"
              effect="dark"
              content="点击查看社团成员"
              placement="top"
            >
              <div
                @click.stop="
                  openCommynityMemberDialog(
                    scope.row.communityId,
                    scope.row.communityName,
                    1
                  )
                "
                class="memberNumber"
              >
                {{ scope.row.memberNumber }}
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" prop="status">
          <template #default="scope">
            <el-tag type="primary" v-if="scope.row.status == 0">正常</el-tag>
            <el-tag type="primary" v-if="scope.row.status == 1">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="280">
          <template #default="scope">
            <el-button
              type="primary"
              text
              size="small"
              @click.stop="
                openCommynityMemberDialog(
                  scope.row.communityId,
                  scope.row.communityName,
                  0
                )
              "
              >申请列表<span v-if="scope.row.applicantNum != 0" class="msg">{{
                scope.row.applicantNum
              }}</span>
            </el-button>

            <el-button
              type="primary"
              text
              size="small"
              @click.stop="openDialog(scope.row.communityId)"
              ><i-ep-edit />编辑
            </el-button>
            <el-popconfirm
              title="你确定需要删除?"
              @confirm="handleDelete(scope.row.communityId)"
            >
              <template #reference>
                <el-button type="danger" text size="small">
                  <i-ep-delete />删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>

    <!-- 社团表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="35%"
      @close="closeDialog"
      style="padding-right: 40px"
    >
      <el-form
        ref="dataFormRef"
        :model="formData"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="社团名称" prop="communityName">
          <el-input
            v-model="formData.communityName"
            placeholder="请输入社团名称"
          />
        </el-form-item>

        <el-form-item label="社长名称" prop="communityPresident">
          <el-input
            v-model="formData.communityPresident"
            placeholder="请输入社长名称"
          />
        </el-form-item>

        <el-form-item label="社团简介" prop="communityDetail">
          <el-input
            v-model="formData.communityDetail"
            type="textarea"
            placeholder="请输入社团简介"
          />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="社团成员数" prop="memberNumber">
          <el-input type="number" v-model="formData.memberNumber" disabled />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 社团成员弹窗 -->
    <el-dialog
      v-model="dialogCommunityMember.visible"
      width="60%"
      @close="dialogCommunityMember.visible = false"
      style="padding-right: 40px"
    >
      <!--  status 1 查看成员  1申请列表-->
      <CommunityMember
        @query="handleQuery"
        v-if="dialogCommunityMember.visible"
        :communityId="communityId"
        :communityName="communityName"
        :status="status"
      />
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  getPageCommunityAPI,
  getFormCommunityAPI,
  addCommunityAPI,
  updateCommunityAPI,
  deleteCommunityAPI,
} from "@/api/community/index";

import { IPage, ICommunity } from "@/api/community/types";

import { ref, reactive } from "vue";

import CommunityMember from "./components/communityMember.vue";

const total = ref(0);
const loading = ref(false);
const tableData = ref<ICommunity[]>([]);
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
const communityList = ref<any[]>([]);

const communityId = ref<number>();
const communityName = ref<string>();
const status = ref<number>();

// 表单数据
const formData = reactive<ICommunity>({
  status: 0,
});
// 表单规则
const rules = reactive({
  title: [{ required: true, message: "请输入", trigger: "blur" }],
});

const dialog = reactive<DialogOption>({
  visible: false,
});

const dialogCommunityMember = reactive<DialogOption>({
  visible: false,
});
const queryParams = reactive<IPage>({
  pageNum: 1,
  pageSize: 10,
});

/* 打开社区成员弹窗 */

const openCommynityMemberDialog = (id: number, name: string, statu: number) => {
  communityId.value = id;
  communityName.value = name;
  status.value = statu;
  dialogCommunityMember.visible = true;
};
onMounted(() => {
  handleQuery();
});

// 获取社团列表
function handleQuery() {
  loading.value = true;
  getPageCommunityAPI(queryParams)
    .then(({ data }) => {
      tableData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */

function resetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 打开社团表单弹窗 */
function openDialog(communityId?: number) {
  if (communityId) {
    dialog.title = "修改社团";
    getFormCommunityAPI(communityId).then(({ data }) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增社团";
  }
  dialog.visible = true;
}

/** 社团保存提交 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const communityId = formData.communityId;
      if (communityId) {
        updateCommunityAPI(communityId, formData)
          .then(() => {
            ElMessage.success("修改成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        addCommunityAPI(formData)
          .then(() => {
            ElMessage.success("新增成功");
            closeDialog();
            resetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 重置表单 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.communityId = undefined;
}
/** 删社团 */
function handleDelete(communityId: number) {
  loading.value = true;
  deleteCommunityAPI(communityId)
    .then(() => {
      ElMessage.success("删除成功");
      resetQuery();
    })
    .finally(() => (loading.value = false));
  resetForm();
}
</script>

<style lang="scss" scoped>
.msg {
  transform: translateY(-1px);
  margin-left: 4px;
  display: inline-block;
  width: 16px;
  height: 16px;
  text-align: center;
  line-height: 16px;
  background-color: #d91b0a;
  color: #fff;
  border-radius: 100%;
}
</style>
